package com.sax.sax.handler;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sax.sax.entity.Record;

public class RecordHandler extends DefaultHandler{
	String value=null;
	Record record=null;
	private String tag=null;
	private List<Record> recordList=new ArrayList<Record>();
	public List<Record> getRecordList(){
		return recordList;
		
	}

	public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		if("RECORD".equals(qName)){
			record=new Record();
		}
		tag=qName;
		
	}
	
	public void characters(char[] ch,int start,int length)throws SAXException{
		SimpleDateFormat formatter = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss");
		Date date;
		if(tag!=null){
			String textData=new String(ch,start,length);
			
			if("id".equals(tag)){
				record.setId(textData);
			}
			else if("article".equals(tag)){
				record.setArticle(textData);
			}
			else if("discuss".equals(tag)){
				record.setDiscuss(Integer.valueOf(textData));
			}
			else if("insertTime".equals(tag)){
					date=new Date();
					try {
						date=formatter.parse(textData);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				record.setInsertTime(date);
			}
			else if("origin".equals(tag)){
				record.setOrigin(textData);
			}
			else if("person_id".equals(tag)){
				record.setPerson_id(textData);
			}
			else if("time".equals(tag)){
					date=new Date();
					try {
						date=formatter.parse(textData);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				record.setTimedate(date);
			}
			else if("transmit".equals(tag)){
				record.setTransmit(Integer.valueOf(textData));
			}
		}
	}
	
	public void endElement(String uri, String localName, String qName)throws SAXException {
		if("RECORD".equals(qName)){
			recordList.add(record);
			record=null;
		}
		tag=null;
	}
	
}
